package com.titaniume.java;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 *@program: JavaSenior
 *@description:
 * Collection接口中声明方法测试
 * 问collection接口的实现类对象中添加数据obj时 要求obj所在类要重写equals()方法
 *
 *
 *@author: renfei.huang
 *@create: 2021-10-11 13:30
 */
public class CollectionTtest {

    @Test
    public void test1(){
        Collection collection = new ArrayList();
        collection.add("aaaa");
        collection.add(123);
        collection.add(456);
        collection.add( new Person("jerry", 20) );
        collection.add(new String("tom") );
//        Person p = new Person("jerry", 123);
        collection.add(true);
//        collection.add(p);

        //1.contains(Object obj) 判断当前集合是否包含obj
        boolean contains = collection.contains(123);
        System.out.println(contains);
        System.out.println(collection.contains(new String("tom")));
//        System.out.println(collection.contains(p)); //true
        //重写Person的equals 才是true
        //没有重写执行的Object的equals 执行的是==
        //我们在判断时候会调用obj对象所在类的equals()
        System.out.println(collection.contains(new Person("jerry", 20)));  //f没有重写-->false

        //2.containsAll(Collection collection) 判断形参collection1中的所有元素是否存在于当前集合中

        Collection collection1 = Arrays.asList(123, 456);
        System.out.println(collection.containsAll(collection1));

    }


    @Test
    public  void test2(){
        //3.remove(Object obj): 当前结合中删除obj元素
        Collection collection = new ArrayList();
        collection.add("aaaa");
        collection.add(123);
        collection.add(456);
        collection.add( new Person("jerry", 20) );
        collection.add(new String("tom") );
        collection.add(true);
        System.out.println(collection.remove(123));
        System.out.println(collection);

        collection.remove(new Person("jerry", 20));
        System.out.println(collection);


        //4.removeAll(Collection collection) : 差集(删除a和b都有的元素) 从当前集合中删除ccollection1中所有元素

        Collection collection1 = Arrays.asList(true,456);
        collection.removeAll(collection1);
        System.out.println(collection);
    }

    @Test
    public  void test3(){
        Collection collection = new ArrayList();
        collection.add("aaaa");
        collection.add(123);
        collection.add(456);
        collection.add( new Person("jerry", 20) );
        collection.add(new String("tom") );
        collection.add(true);

        //5.retainAll(Object obj) : 获取当前结合和collection1集合的交集 ，并返回给当前结合
        Collection collection1 = Arrays.asList(true,456,"cxx");
        collection.retainAll(collection1);
        System.out.println(collection);

    }


    @Test
    public  void test4(){
        Collection collection1 = new ArrayList();
        collection1.add("aaaa");
        collection1.add(123);
        collection1.add(456);
        collection1.add( new Person("jerry", 20) );
        collection1.add(new String("tom") );
        collection1.add(true);


        Collection collection2 = new ArrayList();
        collection2.add("aaaa");
        collection2.add(123);
        collection2.add(456);
        collection2.add( new Person("jerry", 20) );
        collection2.add(new String("tom") );
        collection2.add(true);

        //collection1和collection2 的顺序若不一样也返回false ArrayList 是无序的
        //6.equals(Object obj):要想返回true 需要当前集合和形参集合的元素都相同
        System.out.println(collection1.equals(collection2));

    }


    @Test
    public void test5(){

        Collection collection = new ArrayList();
        collection.add("aaaa");
        collection.add(123);
        collection.add(456);
        collection.add( new Person("jerry", 20) );
        collection.add(new String("tom") );
        collection.add(true);

        //7.hashCode() :返回当前对象的哈希值
        System.out.println(collection.hashCode());


        //8.toArray() 集合----->数组
        Object[] arr = collection.toArray();
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }

        //扩展 ： 数组--->集合 调用Arrays类的静态方法asList()
        List<String>  stringList= Arrays.asList(new String[]{"aa", "bb", "cc"});
        System.out.println(stringList);

        //注意这种写法会变成一个元素(坑)
//        List integers = Arrays.asList(new int[] {123, 343});

        //用下面这2种写法
//        List<Integer> integers = Arrays.asList(123, 343);
          List integers = Arrays.asList(new Integer[] {123, 343});
        System.out.println(integers);


        //iterator(): 返回Iterator接口的实例用于遍历集合元素 ，放在IteratorTest.java中

    }



}
